import { _decorator, Component, Node, sys } from 'cc';
import { platform } from './platform';
const { ccclass, property } = _decorator;

@ccclass('platform_Dy')
export class platform_Dy extends platform {
    private strRewardVideoId = '3w1ieko766digh54d8';
    recordTime: number;
    videoPath: any;
    recorderObj: any;
    recordStartTime: number;
    constructor() {
        super();
        if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            console.error("当前不是字节小游戏运行环境")
        }
        this.InitRewardVideoAd();
        this.InitShare();
    }

    InitRewardVideoAd() {
        if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            console.log("当前不是字节小游戏运行环境")
            return;
        }
        this.adVideoAd = tt.createRewardedVideoAd({ adUnitId: this.strRewardVideoId });
        this.adVideoAd.onLoad(() => {
            console.log('激励视频 广告加载成功')
        })

        this.adVideoAd.onClose(data => {
            tt.hideLoading();
            if (data && data.isEnded) {
                if (this.callBack) {
                    this.callBack(true, "")
                    this.callBack = null;
                }
            }
            else {
                console.log("播放失败 0")
                // 播放中途退出，不下发游戏奖励
                if (this.callBack) {
                    this.callBack(false, "没有完成播放")
                    this.callBack = null;
                }
            }
        })

        this.adVideoAd.onError(err => {
            tt.hideLoading();
            console.log("AdVieo" + err)
        });

        this.adVideoAd.load();
    }


    ShowRewardVideoAd(target: any, callBack: (bool: boolean, str: string) => void) {
        super.ShowRewardVideoAd(target, callBack);
        if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            console.log("当前环境不是字节小程序")
            return;
        }
        if (!this.adVideoAd) return;
        if (!this.callBack) this.callBack = callBack;
        this.adVideoAd.load().then(() => {
            console.log(" this.adVideoAd.load()");
            this.adVideoAd.show().catch(() => {
                console.log(" this.adVideoAd.show()");
                if (this.callBack) {
                    this.callBack(false, "拉取广告失败");
                    this.callBack = null;
                }
            });
        })
    }

    InitShare() {

    }

    CreateBanner(pos) { }

    DestoryBanner() {
    }

    CreateCustomAd(pos) {
    }

    stopRecord() {
        if (this.recorderObj) {
            this.recorderObj.stop();
        }
        this.recordTime = new Date().getTime() - this.recordStartTime;
        console.log("--------   stopRecord");
    }

    startRecord() {
        this.recorderObj = tt.getGameRecorderManager();
        if (this.recorderObj == null) {
            console.log("返回 null 时表示当前 APP 版本不支持录屏")
            return;
        }
        console.log("--------   startRecord");
        this.recorderObj.onStart(res => {
            console.log("录屏开始,res:", res);
        });

        this.recorderObj.onStop(res => {
            console.log("录屏结束,res:", res);
            this.videoPath = res.videoPath;
            this.doRocordEnd();
        });

        this.recorderObj.onPause(() => {
            console.log("监听录屏暂停事件");
        });

        this.recorderObj.onResume(() => {
            console.log("监听录屏继续事件");
        });

        this.recorderObj.onError((error) => {
            this.doRocordEnd();
        });

        this.recorderObj.start({
            duration: 300
        });
        this.recordStartTime = new Date().getTime();
        this.videoPath = null;
    }
    doRocordEnd() {
        throw new Error('Method not implemented.');
    }

    canShare() {
        return this.videoPath;
    }

    Share(success, fail?) {
        console.log("dyshare");
        if (this.recordTime <= 5 * 1000) {
            console.log('录屏失败：录屏时长低于 3 秒');
            //Toast.create('录屏失败：录屏时长低于 3 秒');
            return;
        }
        if (!this.videoPath) {
            console.log('录屏失败：录屏时长低于 3 秒');
            //Toast.create('录屏失败：录屏时长低于 3 秒');
            return;
        }
        console.log('#####videoPath:', this.videoPath)
        let shareTemplateId = "1bq22cie8i521c8ij4";
        let gameInfo: string = null;
        let thisObj = this;
        let obj = {
            templateId: shareTemplateId, // 替换成通过审核的分享ID
            channel: "video",
            // title: shareData.title,
            // desc: shareData.title,
            // query: gameInfo + '&shareImg=' + shareData.imageUrl,
            // imageUrlId: shareData.imageUrlId,
            // imageUrl: shareData.imageUrl,
            extra: {
                videoPath: this.videoPath
            },
            success() {
                if (success) {
                    success();
                }
                // Toast.create('分享成功')
                thisObj.videoPath = null;
                thisObj.recorderObj = null;
                console.log("分享成功");
            },
            fail(e) {
                if (fail) {
                    fail();
                }
                console.log("分享失败,e:", e);
            }
        }
        console.log("------------------ tt.shareAppMessage(obj),obj:", obj);
        tt.shareAppMessage(obj);
    }


}
declare let tt: any;

